<?php


namespace app\api\controller\v20;


use app\api\controller\v2\Auth;
use app\common\cachemodel\CouponCode;
use app\common\cachemodel\ZlCouponActive;
use app\common\lib\Util;
use XznsTools\cache\CacheManage;

/**
 * @version 2.3.0
 * Class YearActivSix
 * @package app\api\controller\v20
 */
class YearActivSix extends Auth
{
    /**
     * 2020年春节活动领取优惠券
     * param $dt int
     * $dt=>6    2020-09-06   月度报告免费券 30张
     * $dt=>7    2020-09-07   恋爱报告免单 20张
     * $dt=>8    2020-09-08   幸运地图免单 20张
     * $dt=>9    2020-09-09   年运免单  20 张
     * $dt=>10    2020-09-10   财富报告免单
     * @throws \Exception
     */
    public function getCoupon()
    {
        $dt = input('dt/d', 0);

        $dateArr = [
            6 => [ // 2020-09-06
                'date' => 1599386400, // 活动开始时间戳 2020-09-06 18:00:00
                'end_date' => 1599408000, // 活动结束时间戳 2020-09-07 00:00:00
                'group_id' => 22  // 兑换码组号
            ],
            7 => [ // 2020-09-07
                'date' => 1599472800, // 活动开始时间戳 2020-09-07 18:00:00
                'end_date' => 1599494400, // 活动结束时间戳 2020-09-08 00:00:00
                'group_id' => 23  // 兑换码组号
            ],
            8 => [ // 2020-09-08
                'date' => 1599559200, // 活动开始时间戳 2020-09-08 18:00:00
                'end_date' => 1599580800, // 活动结束时间戳 2020-09-09 00:00:00
                'group_id' => 24  // 兑换码组号
            ],
            9 => [ // 2020-09-09
                'date' => 1599645600, // 活动开始时间戳 2020-09-09 18:00:00
                'end_date' => 1599667200, // 活动结束时间戳 2020-09-10 00:00:00
                'group_id' => 25  // 兑换码组号
            ],
            10 => [ // 2020-09-10
                'date' => 1599732000, // 活动开始时间戳 2020-09-10 18:00:00
                'end_date' => 1599753600, // 活动结束时间戳 2020-09-11 00:00:00
                'group_id' => 26  // 兑换码组号
            ],
        ];

        if (!$dt || !isset($dateArr[$dt]))
        {
            return show(config('code.error'), '活动暂未开始');
        }

        $time = Util::testGetTime();

        if ($time > $dateArr[$dt]['end_date'])
        {
            return show(config('code.error'), '活动已结束');
        }
        if ($time < $dateArr[$dt]['date'])
        {
            return show(config('code.error'), '活动暂未开始');
        }
        // 获取redis操作对象
        $redis = CacheManage::getInstance()->redisHandler;
        // 获取领取过优惠券的用户
        $giveData = $redis->lRange('six_year:activ_send_uid:' . $dateArr[$dt]['group_id'], 0, -1);
        if (in_array($this->user['id'], $giveData))
        {
            return show(config('code.error'), '已经领过过了');
        }
        // 缓存KEY
        $cacheKey = 'six_year:activ:' . $dateArr[$dt]['group_id'];
        // 获取优惠券剩余数量
        $couponNum = $redis->lLen($cacheKey);
        if ($couponNum)
        {
            $code = $redis->lPop($cacheKey);
            if ($code)
            { // 抢到了优惠券
                $couponCodeModel = new CouponCode();
                $res = $couponCodeModel->codeGet($this->user['id'], $code);
                // 记录领取用户UID
                $redis->lPush('six_year:activ_send_uid:' . $dateArr[$dt]['group_id'], $this->user['id']);
                // 更新领取数量
                $redis->hIncrBy('six_year:activ_send_num', $dateArr[$dt]['group_id'], 1);
                if ($res)
                {
                    $this->giveRecordCacheAdd($this->user, $dateArr[$dt]['group_id'], 2);
                    return show(config('code.success'), '已存放至"我的-卡券中心"');
                } else
                {
                    return show(config('code.error'), '优惠券已空');
                }
            }
        }

        return show(config('code.error'), '优惠券已空');
    }

    public function getActivCoupon()
    {
        $dt = input('dt/d', 0);

        $arr = [
            6 => 10,
            7 => 11,
            8 => 12,
            9 => 13,
            10 => 14,
        ];

        if (!$dt && !isset($arr[$dt]))
        {
            return show(config('code.error'), '活动暂未开始');
        }

        $couponActiveModel = new ZlCouponActive();

        $res = $couponActiveModel->couponGiveActiv((int)$this->user['id'], $arr[$dt]);

        if ($res)
        {
            $this->giveRecordCacheAdd($this->user, $arr[$dt], 1);
            return show(config('code.success'), '已存放至"我的-卡券中心"');
        } else
        {
            return show(config('code.error'), '优惠券已空');
        }
    }

    /**
     * 记录领取记录
     * @param array $userinfo
     * @param int $id
     * @param int $type 1活动 2抢
     */
    private function giveRecordCacheAdd(array $userinfo, int $id, int $type = 1)
    {
        $len = 15;
        // 获取redis操作对象
        $redis = CacheManage::getInstance()->redisHandler;

        $cacheKey = 'six_year:give:info';
        $num = $redis->lLen($cacheKey);

        if ($num >= $len)
        {
            $redis->rPop($cacheKey);
        }

        $info = [
            'nickname' => $userinfo['nickname'],
            'avatar' => $userinfo['avatar'],
            'id' => $id,
            'type' => $type,
        ];

        $record = json_encode($info);

        $redis->lPush($cacheKey, $record);
    }
}
